package com.aolong.archives.common.util;

import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import com.aolong.archives.common.log.BaseLogger;
import com.rock.platform.exception.BaseException;
import com.rock.platform.util.JpaDialectUtils;

public class InvokeWebService
{
    public static String invokeIfService(String url,String nameSpace,String ifUser, String ifPass, String ifCode, String ifEvent,
            String info)
    {
        StringBuffer msg = new StringBuffer();
        msg.append("调用外部系统出口,ifUser:"+ifUser+";ifPass:"+ifPass+";ifCode:"+ifCode+";ifEvent:"+ifEvent+"\r\n");
        msg.append("info:"+info);
        
        try
        {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setOperationName(new QName(nameSpace, "IfService"));

            call.setTargetEndpointAddress(new java.net.URL(url));
            call.addParameter(new QName(nameSpace, "ifUser"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(nameSpace, "ifPass"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(nameSpace, "ifCode"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(nameSpace, "ifEvent"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(nameSpace, "info"), XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);
            call.setReturnClass(String.class);
            
            call.setUseSOAPAction(true); 
            call.setSOAPActionURI(nameSpace + "IfService");

            String k = (String) call.invoke(new Object[]
            { ifUser, ifPass, ifCode, ifEvent, info });
            System.out.println(k);

            
            return k;
        }
        catch (Exception e)
        {
            String ifError = JpaDialectUtils.getException(e);
            String ifErrorStack = e.toString();
            throw new BaseException(e);
        }
    }

    public String invokeService(String url,String nameSpace,String webmethod, Map<String, ? extends Object> paraVal)
    {
        StringBuffer msg = new StringBuffer();
        msg.append("调用外部系统出口,");
        msg.append("webmethod:"+webmethod+";");
        
        try
        {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setOperationName(new QName(nameSpace, webmethod));

            call.setTargetEndpointAddress(new java.net.URL(url));
            
            Object[] oKey = paraVal.keySet().toArray();
            for (Object key : oKey)
            {
                call.addParameter(new QName(nameSpace, key.toString()), XMLType.XSD_STRING, ParameterMode.IN);
                msg.append(key.toString()+":"+paraVal.get(key).toString()+";");
            }
            call.setReturnType(XMLType.XSD_STRING);
            call.setReturnClass(String.class);
            
            call.setUseSOAPAction(true); 
            call.setSOAPActionURI(nameSpace + webmethod);

            String k = (String) call.invoke(paraVal.values().toArray());
            System.out.println(k);

            return k;
        }
        catch (Exception e)
        {
            String ifError = JpaDialectUtils.getException(e);
            String ifErrorStack = e.toString();
            throw new BaseException(e);
        }
    }
    
}
